package cn.gcyl.module.biz.service.collect;

import cn.gcyl.framework.common.pojo.PageResult;
import cn.gcyl.framework.common.util.object.BeanUtils;
import cn.gcyl.module.biz.controller.admin.collect.vo.*;
import cn.gcyl.module.biz.dal.dataobject.collect.CollectDO;
import cn.gcyl.module.biz.dal.mysql.collect.CollectMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static cn.gcyl.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.gcyl.module.system.enums.ErrorCodeConstants.COLLECT_NOT_EXISTS;

/**
 * 收藏信息 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class CollectServiceImpl implements CollectService {

    @Resource
    private CollectMapper collectMapper;

    @Override
    public Long createCollect(CollectSaveReqVO createReqVO) {
        // 插入
        CollectDO collect = BeanUtils.toBean(createReqVO, CollectDO.class);
        collectMapper.insert(collect);

        // 返回
        return collect.getId();
    }

    @Override
    public void updateCollect(CollectSaveReqVO updateReqVO) {
        // 校验存在
        validateCollectExists(updateReqVO.getId());
        // 更新
        CollectDO updateObj = BeanUtils.toBean(updateReqVO, CollectDO.class);
        collectMapper.updateById(updateObj);
    }

    @Override
    public void deleteCollect(Long id) {
        // 校验存在
        validateCollectExists(id);
        // 删除
        collectMapper.deleteById(id);
    }

    @Override
        public void deleteCollectListByIds(List<Long> ids) {
        // 删除
        collectMapper.deleteByIds(ids);
        }


    private void validateCollectExists(Long id) {
        if (collectMapper.selectById(id) == null) {
            throw exception(COLLECT_NOT_EXISTS);
        }
    }

    @Override
    public CollectDO getCollect(Long id) {
        return collectMapper.selectById(id);
    }

    @Override
    public PageResult<CollectDO> getCollectPage(CollectPageReqVO pageReqVO) {
        return collectMapper.selectPage(pageReqVO);
    }

}